#include "async_client.h"
#include "globals.h"

namespace async_client {

AsyncClient::AsyncClient(boost::asio::io_service& io_service, const std::string& server, int16_t port)
	:TcpClient(io_service, server, port)
{

};

AsyncClient::~AsyncClient()
{

}

void AsyncClient::process_read()
{
	istream ss(&response_);
	std::string tmp;
	ss >> tmp;
	if (tmp == "ping") 
	{
		shared_ptr<PING_INFO> ping_info = shared_ptr<PING_INFO>(new PING_INFO);
		ping_info->client = this;
		stringstream ss2;
		ss >> tmp;
		ss2 << tmp;
		while (ss >> tmp) 
		{
			ss2 << " "<< tmp;
		}
		ping_info->message = ss2.str();
		eventDriver.emitEvent("ping", ping_info);
	}
	else if (tmp == "pong")
	{
		
		shared_ptr<PING_INFO> pong_info = shared_ptr<PING_INFO>(new PING_INFO);
		pong_info->client = this;
		stringstream ss2;
		ss >> tmp;
		ss2 << tmp;
		while (ss >> tmp) 
		{
			ss2 << " "<< tmp;
		}
		pong_info->message = ss2.str();
		
		eventDriver.emitEvent("pong", pong_info);
	}
	else
	{
		shared_ptr<UNDEFINED_MSG_INFO> undef_info = shared_ptr<UNDEFINED_MSG_INFO>(new UNDEFINED_MSG_INFO);
		undef_info->client = this;
		stringstream ss2;
		ss2 << tmp << " ";
		ss >> tmp;
		ss2 << tmp;
		while (ss >> tmp) 
		{
			ss2 << " "<< tmp;
		}
		undef_info->message = ss2.str();
		eventDriver.emitEvent("undef", undef_info);
	}
}

}; // namespace async_client